package com.whnk.web.controller.workflow;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.whnk.common.annotation.Log;
import com.whnk.common.enums.BusinessType;
import com.whnk.workflow.domain.Leave;
import com.whnk.workflow.service.ILeaveService;
import com.whnk.framework.util.ShiroUtils;
import com.whnk.framework.web.base.BaseController;
import com.whnk.framework.web.page.TableDataInfo;
import com.whnk.common.base.AjaxResult;

/**
 * 请假 信息操作处理
 * 
 * @author whwhnk
 * @date 2019-04-08
 */
@Controller
@RequestMapping("/workflow/leave")
public class LeaveController extends BaseController
{
    private String prefix = "workflow/leave";
	
	@Autowired
	private ILeaveService leaveService;
	
	@RequiresPermissions("workflow:leave:view")
	@GetMapping()
	public String leave()
	{
	    return prefix + "/leave";
	}
	
	/**
	 * 查询请假列表
	 */
	@RequiresPermissions("workflow:leave:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Leave leave)
	{
		startPage();
		leave.setLoginName(ShiroUtils.getLoginName());
        List<Leave> list = leaveService.selectLeaveList(leave);
		return getDataTable(list);
	}
	
	/**
	 * 新增请假
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存请假
	 */
	@RequiresPermissions("workflow:leave:add")
	@Log(title = "请假", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Leave leave)
	{
		return toAjax(leaveService.insertLeave(leave));
	}
	
	/**
	 * 修改请假
	 */
	@GetMapping("/edit/{businessKey}")
	public String edit(@PathVariable("businessKey") String businessKey, ModelMap mmap)
	{
		Leave leave = leaveService.selectLeaveById(businessKey);
		mmap.put("leave", leave);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存请假
	 */
	@RequiresPermissions("workflow:leave:edit")
	@Log(title = "请假", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Leave leave)
	{
		return toAjax(leaveService.updateLeave(leave));
	}
	
	/**
	 * 删除请假
	 */
	@RequiresPermissions("workflow:leave:remove")
	@Log(title = "请假", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		return toAjax(leaveService.deleteLeaveByIds(ids));
	}
}
